package com.jdk.juc.tools;

import java.util.Random;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;

public class CyclicBarrierTest {
    public static void main(String[] args) {
        CyclicBarrier barrier = new CyclicBarrier(3);
        for(int i = 0; i < barrier.getParties(); i++){
            new Thread(()->{
                Random rand = new Random();
                int randomNum = rand.nextInt((3000 - 1000) + 1) + 1000;
                try {
                    TimeUnit.MILLISECONDS.sleep(randomNum);
                    System.out.println(Thread.currentThread().getName() + ", CyclicBarrier Test "+((double)randomNum/1000)+"s");
                    barrier.await();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }, "队友"+i).start();
        }
        System.out.println("CyclicBarrier Test is finished.");
    }

}
